﻿using System;
using UnityEngine;

public class ConvertHelper
{ 
    public static object ChangeType(string value ,Type conversionType)
    {
        if (string.IsNullOrEmpty(value))
            return default;

        if (conversionType == typeof(string))
            return value;

        if (conversionType.IsArray)
        {
            string[] array1 = value.Split('#');
            if (conversionType.Name == "String[]")
            {
                return array1;
            }
            if (conversionType.Name == "Int32[]")
            {
                int[] array2 = new int[array1.Length];
                for (int n = 0; n < array1.Length; n++)
                {
                    array2[n] = int.Parse(array1[n]);
                }
                return array2;
            }
            if (conversionType.Name == "Single[]")
            {
                float[] array2 = new float[array1.Length];
                for (int n = 0; n < array1.Length; n++)
                {
                    array2[n] = float.Parse(array1[n]);
                }
                return array2;
            }
            if (conversionType.Name == "Int16[]")
            {
                short[] array2 = new short[array1.Length];
                for (int n = 0; n < array1.Length; n++)
                {
                    array2[n] = short.Parse(array1[n]);
                }
                return array2;
            }
            if (conversionType.Name == "Byte[]")
            {
                byte[] array2 = new byte[array1.Length];
                for (int n = 0; n < array1.Length; n++)
                {
                    array2[n] = byte.Parse(array1[n]);
                }
                return array2;
            }
            if (conversionType.Name == "Int64[]")
            {
                long[] array2 = new long[array1.Length];
                for (int n = 0; n < array1.Length; n++)
                {
                    array2[n] = long.Parse(array1[n]);
                }
                return array2;
            }
            if (conversionType.Name == "Double[]")
            {
                double[] array2 = new double[array1.Length];
                for (int n = 0; n < array1.Length; n++)
                {
                    array2[n] = double.Parse(array1[n]);
                }
                return array2;
            }
        }

        if (conversionType.Name == "Vector2")
        {
            var array = value.Split('#');
            if (array.Length != 2)
                return Vector2.zero;
            return new Vector2(float.Parse(array[0]), float.Parse(array[1]));
        }
        else if (conversionType.Name == "Vector3")
        {
            var array = value.Split('#');
            if (array.Length != 2)
                return Vector2.zero;
            return new Vector3(float.Parse(array[0]), float.Parse(array[1]), float.Parse(array[2]));
        }
        else if (conversionType.Name == "Vector2Int")
        {
            var array = value.Split('#');
            if (array.Length != 2)
                return Vector2Int.zero;
            return new Vector2Int(int.Parse(array[0]), int.Parse(array[1]));
        }
        else if (conversionType.Name == "Vector3Int")
        {
            var array = value.Split('#');
            if (array.Length != 3)
                return Vector3Int.zero;
            return new Vector3Int(int.Parse(array[0]), int.Parse(array[1]), int.Parse(array[2]));
        }
        else if(conversionType.Name == "Quaternion")
        {
            var array = value.Split('#');
            if (array.Length != 3)
                return Quaternion.identity;
            return Quaternion.Euler(float.Parse(array[0]), float.Parse(array[1]), float.Parse(array[2]));
        }

        return Convert.ChangeType(value, conversionType);
    }
}
